package com.zjdiepu.www.biz.yw;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.status.AuditListStatus;
import com.zjdiepu.www.base.enums.status.CarinfoEditStatus;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.ModeIDType;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.dz.DzRegistrationMapper;
import com.zjdiepu.www.dao.other.OtherAuditInfoMapper;
import com.zjdiepu.www.dao.yw.YwBusinessCarMapper;
import com.zjdiepu.www.dao.yw.YwCarinfoEditHisMapper;
import com.zjdiepu.www.dao.yw.YwCarinfoEditMapper;
import com.zjdiepu.www.ibiz.yw.IYwCarinfoEditService;
import com.zjdiepu.www.model.base.dto.SysUser;
import com.zjdiepu.www.model.dz.dto.DzRegistration;
import com.zjdiepu.www.model.other.dto.OtherAuditInfo;
import com.zjdiepu.www.model.yw.dto.YwBusinessCar;
import com.zjdiepu.www.model.yw.dto.YwCarinfoEdit;
import com.zjdiepu.www.model.yw.dto.YwCarinfoEditHis;
import com.zjdiepu.www.model.yw.ext.YwCarinfoEditExt;
import com.zjdiepu.www.util.ExceptionHelper;

/**
 * yw_车辆信息修改Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2018年01月29日 15时59分<br/>
 * @since 1.0<br/>
 */
@Service
public class YwCarinfoEditServiceImpl extends BaseService implements IYwCarinfoEditService {
	public static final Logger logger = LoggerFactory.getLogger(YwCarinfoEditServiceImpl.class);

	@Autowired
	private YwCarinfoEditMapper ywCarinfoEditMapper;
	@Autowired
	private YwCarinfoEditHisMapper ywCarinfoEditHisMapper;
	@Autowired
	private DzRegistrationMapper dzRegistrationMapper;
	@Autowired
	private YwBusinessCarMapper ywBusinessCarMapper;
	@Autowired
	private OtherAuditInfoMapper otherAuditInfoMapper;
	
	@Override
	public boolean saveAudit(String busiId, boolean pass, SysUser sysUser) {
		YwCarinfoEdit ywCarinfoEdit = new YwCarinfoEdit();
	    ywCarinfoEdit.setDpStatus(DpStatus.NORMAL.getCode());
	    ywCarinfoEdit.setBusiId(busiId);
	    ywCarinfoEdit.setCarinfoEditStatus(CarinfoEditStatus.DAI_SHE_HE.getCode());
	    ywCarinfoEdit = ywCarinfoEditMapper.selectRefined(ywCarinfoEdit);
	    if(null == ywCarinfoEdit) {
	    	ExceptionHelper.thow(ResultFlagType.MISSING.getCode().toString(), "申请车辆信息修改记录丢失");
	    }
	    ywCarinfoEdit.initUpdateData(sysUser.getId(), sysUser.getName());
		OtherAuditInfo otherAuditInfo = new OtherAuditInfo();
		otherAuditInfo.initInsertData(sysUser.getId(), sysUser.getName());
		otherAuditInfo.setModeId(ModeIDType.CLXXXGSH.getCode());
		otherAuditInfo.setBusiId(busiId);
		otherAuditInfo.setDataId(ywCarinfoEdit.getId());
		otherAuditInfo.setAuditName(ModeIDType.CLXXXGSH.getValue());
		otherAuditInfo.setOrgCode(sysUser.getOrgId());
		otherAuditInfo.setOrgName(sysUser.getOrgName());
		
		if(pass) {
			otherAuditInfo.setAuditStatus(AuditListStatus.SHEN_PI_TONG_GUO.getCode().toString());
			ywCarinfoEdit.setCarinfoEditStatus(CarinfoEditStatus.SHEN_HE_TONG_GUO.getCode());
			
			YwBusinessCar ywBusinessCar = new YwBusinessCar();
    		ywBusinessCar.setBusiId(busiId);
    		ywBusinessCar.setDpStatus(DpStatus.NORMAL.getCode());
    		ywBusinessCar = ywBusinessCarMapper.selectRefined(ywBusinessCar);
    		if(null == ywBusinessCar) {
    			ExceptionHelper.thow(String.valueOf(ResultFlagType.MISSING.getCode()), "车辆记录缺失");
    		}
    		String carId = ywBusinessCar.getId();
			BeanUtils.copyProperties(ywCarinfoEdit, ywBusinessCar);
			ywBusinessCar.initUpdateData(sysUser.getId(), sysUser.getName());
			ywBusinessCar.setId(carId);;
			ywBusinessCarMapper.updateByPrimaryKeySelective(ywBusinessCar);
			
			DzRegistration dzRegistration = new DzRegistration();
			dzRegistration.setBusiId(busiId);
			dzRegistration.setDpStatus(DpStatus.NORMAL.getCode());
			dzRegistration = dzRegistrationMapper.selectRefined(dzRegistration);
			if(null != dzRegistration) {
				String registrationId = dzRegistration.getId();
				BeanUtils.copyProperties(ywCarinfoEdit, dzRegistration);
				dzRegistration.initUpdateData(sysUser.getId(), sysUser.getName());
				dzRegistration.setId(registrationId);
				dzRegistrationMapper.updateByPrimaryKeySelective(dzRegistration);
			}
			
		} else {
			otherAuditInfo.setAuditStatus(AuditListStatus.SHEN_PI_BU_TONG_GUO.getCode().toString());
			ywCarinfoEdit.setCarinfoEditStatus(CarinfoEditStatus.SHEN_HE_BU_TONG_GUO.getCode());
		}
		otherAuditInfoMapper.insert(otherAuditInfo);
		ywCarinfoEditMapper.updateByPrimaryKeySelective(ywCarinfoEdit);
		return true;
	}
	
	@Override
	public void saveUpdateAndDelete(YwCarinfoEdit ywCarinfoEdit, OperateType operType) {
		if(ywCarinfoEdit.getCommit() != null && ywCarinfoEdit.getCommit()) {
			ywCarinfoEdit.setCarinfoEditStatus(CarinfoEditStatus.DAI_SHE_HE.getCode());
		} else {
			ywCarinfoEdit.setCarinfoEditStatus(CarinfoEditStatus.YI_BAO_CUN.getCode());
		}
		switch (operType) {
		case add:
			ywCarinfoEdit.initInsertData(ywCarinfoEdit.getModifier(), ywCarinfoEdit.getModifierName());
			ywCarinfoEditMapper.insert(ywCarinfoEdit);
			//插入历史记录
			saveCarinfoEditHis(ywCarinfoEdit.getBusiId(), ywCarinfoEdit.getId(), ywCarinfoEdit.getModifier(), ywCarinfoEdit.getModifierName());
			break;
		case edit:
			ywCarinfoEdit.initUpdateData(ywCarinfoEdit.getModifier(), ywCarinfoEdit.getModifierName());
			// 影响的记录数
			int updateCount = ywCarinfoEditMapper.updateByPrimaryKeySelective(ywCarinfoEdit);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			ywCarinfoEdit.initDeleteData(ywCarinfoEdit.getModifier(), ywCarinfoEdit.getModifierName());
			// 影响的记录数
			int deleteCount = ywCarinfoEditMapper.updateByPrimaryKeySelective(ywCarinfoEdit);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	private void saveCarinfoEditHis(String busiId, String editId, String userId, String modifierName) {
		YwCarinfoEditHis ywCarinfoEditHis = new YwCarinfoEditHis();
		ywCarinfoEditHis.setDpStatus(DpStatus.NORMAL.getCode());
		ywCarinfoEditHis.setBusiId(busiId);
		ywCarinfoEditHis.setEditId(editId);
		ywCarinfoEditHis = ywCarinfoEditHisMapper.selectRefined(ywCarinfoEditHis);
		if(null == ywCarinfoEditHis) {
			ywCarinfoEditHis = new YwCarinfoEditHis();
			
    		YwBusinessCar ywBusinessCar = new YwBusinessCar();
    		ywBusinessCar.setBusiId(busiId);
    		ywBusinessCar.setDpStatus(DpStatus.NORMAL.getCode());
    		ywBusinessCar = ywBusinessCarMapper.selectRefined(ywBusinessCar);
    		if(null == ywBusinessCar) {
    			ExceptionHelper.thow(String.valueOf(ResultFlagType.MISSING.getCode()), "车辆记录缺失");
    		}
    		BeanUtils.copyProperties(ywBusinessCar, ywCarinfoEditHis);
    		
    		DzRegistration dzRegistration = new DzRegistration();
    		dzRegistration.setBusiId(busiId);
    		dzRegistration.setDpStatus(DpStatus.NORMAL.getCode());
    		dzRegistration = dzRegistrationMapper.selectRefined(dzRegistration);
    		if(null != dzRegistration) {
    			ywCarinfoEditHis.setDvm(dzRegistration.getDvm());
    			ywCarinfoEditHis.setDateRegistrationFirst(dzRegistration.getDateRegistrationFirst());
    			ywCarinfoEditHis.setDateRegistrationNow(dzRegistration.getDateRegistrationNow());
    			ywCarinfoEditHis.setLicensePlate(dzRegistration.getLicensePlate());
    			ywCarinfoEditHis.setRegistrationNumber(dzRegistration.getRegistrationNumber());
    			ywCarinfoEditHis.setRegistrationArea(dzRegistration.getRegionRegistration());
    		}
    		ywCarinfoEditHis.initInsertData(userId, modifierName);
    		ywCarinfoEditHis.setBusiId(busiId);
    		ywCarinfoEditHis.setEditId(editId);
    		ywCarinfoEditHisMapper.insert(ywCarinfoEditHis);
		}
	}
	
	@Override
	public List<YwCarinfoEdit> selectByExt(YwCarinfoEditExt ywCarinfoEditExt) {
		return ywCarinfoEditMapper.selectByExt(ywCarinfoEditExt);
	}
	
	@Override
	public int countByExt(YwCarinfoEditExt ywCarinfoEditExt){
		return ywCarinfoEditMapper.countByExt(ywCarinfoEditExt);
	}
	
	@Override
	public YwCarinfoEdit selectById(String id) {
		return ywCarinfoEditMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			ywCarinfoEditMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(YwCarinfoEdit ywCarinfoEdit) {
		return ywCarinfoEditMapper.countRefined(ywCarinfoEdit);
	}

	@Override
	public YwCarinfoEdit selectRefined(YwCarinfoEdit ywCarinfoEdit) {
		return ywCarinfoEditMapper.selectRefined(ywCarinfoEdit);
	}
	
	@Override
	public int deletedRefined(YwCarinfoEdit ywCarinfoEdit) {
		return ywCarinfoEditMapper.deletedRefined(ywCarinfoEdit);
	}

	@Override
	public boolean repeat(YwCarinfoEdit ywCarinfoEdit) {
		int count = countRefined(ywCarinfoEdit);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(ywCarinfoEdit.getId())) {
			return true;
		}
		YwCarinfoEdit dbItem = selectRefined(ywCarinfoEdit);
		if(null == dbItem) {
			return false;
		}
		return !ywCarinfoEdit.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return ywCarinfoEditMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<YwCarinfoEdit> ywCarinfoEdits) {
		return ywCarinfoEditMapper.insertBatch(ywCarinfoEdits);
	}
}
